/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.module;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.HiddenFileFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class FileExplorerBean {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private Map<String, String> folderStack = new LinkedHashMap<String, String>();
    private Map<String, String> currentFiles = new LinkedHashMap<String, String>();
    private String currentFolderPath;
    private File selectedFile;
    private String selectedFileName;
    private String fileContent;
    
    public void onFileSelect() {
        selectedFile = new File(currentFolderPath + File.separator + selectedFileName);
        if (selectedFile.isDirectory()) {
            fileContent = "";
            currentFolderPath += File.separator + selectedFileName;
            String[] fileNames = selectedFile.list(HiddenFileFilter.VISIBLE);
            for (String fileName : fileNames) {
                currentFiles = new LinkedHashMap<String, String>();
                currentFiles.put(fileName, fileName);
            }
        } else if (selectedFile.isFile()) {
            try {
                fileContent = FileUtils.readFileToString(selectedFile);
            } catch (IOException ex) {
                logger.error("", ex);
            }
        }
    }
}
